package com.dhee.creditcardsystem.controller;

import com.dhee.creditcardsystem.model.Users;
import com.dhee.creditcardsystem.service.UsersService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.sql.SQLException;


@Controller
public class UsersController {
    @Autowired
    private UsersService usersService;



    @RequestMapping("tologin")
    public String tologin(String registerSuccess, HttpSession session, Model model) {
//
//        String registerSuccess = (String) session.getAttribute("registerSuccess");
//
//        // 也检查session属性（保持原有功能）
//        if (registerSuccess == null) {
//            registerSuccess = (String) session.getAttribute("registerSuccess");
//        }
//
//        if ("true".equals(registerSuccess)) {
//            model.addAttribute("registerSuccess", true);
//        }
        return "apply/A1A01WA01A01 入会申込情報入力";
    }

    @RequestMapping("login")
    public String login(Users users, Model model, HttpSession session) throws SQLException {
        //调用业务逻辑
        if ( usersService.login(users) != null) {
            System.out.println("登录成功");

            Users u = usersService.login(users);
            System.out.println(u.getCstid());
            session.setAttribute("cstid", u.getCstid());



            return "/apply/A1A01WA01A01 入会申込情報入力";
        } else {
            model.addAttribute("msg", "用户名或密码错误");
            System.out.println("登录失败");
            return "error";

        }
    }





}
